package com.bagelly.mvvm.util.core

import android.widget.ImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.DrawableTransformation
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.transition.DrawableCrossFadeFactory
import jp.wasabeef.glide.transformations.RoundedCornersTransformation

/**
 *
 * @ProjectName: MyMVVM
 * @Package: com.bagelly.mvvm.util.core
 * @ClassName: ImageLoader
 * @Description: java类作用描述
 * @Author: bagelly
 * QQ:774169396
 * @CreateDate: 2020/6/28 下午4:07
 */

fun ImageView.loadImage(url: String?,imageOptions:ImageOptions?=null){
  Glide.with(context)
      .load(url)
      .apply(requestOptions(imageOptions))
      .transition(
          DrawableTransitionOptions.with(
              DrawableCrossFadeFactory
                  .Builder(300)
                  .setCrossFadeEnabled(true)
                  .build()
          )
      )
      .into(this)


}

fun requestOptions(imageOptions: ImageOptions?)= RequestOptions().apply {
    imageOptions?.let {
        transform(RoundedCornersTransformation(it.cornersRadius, 0))
        placeholder(it.placeholder)
        error(it.error)
        fallback(it.fallback)
        if (it.circleCrop) {
            circleCrop()
        }
    }

}
class ImageOptions {
    var placeholder = 0         // 加载占位图
    var error = 0               // 错误占位图
    var fallback = 0            // null占位图
    var cornersRadius = 0       // 圆角半径
    var circleCrop = false      // 是否裁剪为圆形
}